package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;

import Logic.EmailerJob;

/**
 * Servlet implementation class Emailer
 */
@WebServlet("/Emailer")
public class Emailer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Emailer() {
        super();
        // TODO Auto-generated constructor stub
    } 
      
    public void init() throws ServletException {
    	try{
    		SchedulerFactory schFact = new org.quartz.impl.StdSchedulerFactory();
        	Scheduler sched = schFact.getScheduler();
        	JobDetail job =  new JobDetail("job1", "group1", EmailerJob.class);
        	
        	//only fire trigger once a day 
        	CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/20 * * * * ?");
        	
        	sched.start();	
        	sched.scheduleJob(job, trigger);
    	} catch (Exception e){
    		System.err.println("Scheduler did not trigger");
    		e.printStackTrace();
    	}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
